package com.galaxy.map.controller;

import com.galaxy.common.core.domain.R;
import com.galaxy.map.model.dto.LocationDTO;
import com.galaxy.map.model.dto.PoiDTO;
import com.galaxy.map.model.vo.DistrictVO;
import com.galaxy.map.model.vo.GeocodeVO;
import com.galaxy.map.model.vo.RegeocodeVO;
import com.galaxy.map.model.vo.RouteVO;
import com.galaxy.map.service.IMapService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地图服务控制器
 */
@Tag(name = "地图服务接口")
@RestController
@RequestMapping("/map")
@RequiredArgsConstructor
public class MapController {

    private final IMapService mapService;

    @Operation(summary = "地理编码（地址转坐标）")
    @GetMapping("/geocode")
    public R<GeocodeVO> geocode(
            @Parameter(description = "地址") @RequestParam String address,
            @Parameter(description = "城市（可选）") @RequestParam(required = false) String city) {
        return R.ok(mapService.geocode(address, city));
    }

    @Operation(summary = "逆地理编码（坐标转地址）")
    @GetMapping("/regeocode")
    public R<RegeocodeVO> regeocode(
            @Parameter(description = "经度") @RequestParam Double longitude,
            @Parameter(description = "纬度") @RequestParam Double latitude) {
        LocationDTO location = new LocationDTO();
        location.setLongitude(longitude);
        location.setLatitude(latitude);
        return R.ok(mapService.regeocode(location));
    }

    @Operation(summary = "获取行政区划信息")
    @GetMapping("/districts")
    public R<List<DistrictVO>> getDistricts(
            @Parameter(description = "行政区关键字") @RequestParam String keywords,
            @Parameter(description = "返回下级行政区级数") @RequestParam(defaultValue = "1") Integer subdistrict) {
        return R.ok(mapService.getDistricts(keywords, subdistrict));
    }

    @Operation(summary = "周边搜索")
    @GetMapping("/around")
    public R<List<PoiDTO>> searchAround(
            @Parameter(description = "经度") @RequestParam Double longitude,
            @Parameter(description = "纬度") @RequestParam Double latitude,
            @Parameter(description = "搜索关键字") @RequestParam String keywords,
            @Parameter(description = "搜索半径（单位：米）") @RequestParam(defaultValue = "1000") Integer radius) {
        LocationDTO location = new LocationDTO();
        location.setLongitude(longitude);
        location.setLatitude(latitude);
        return R.ok(mapService.searchAround(location, keywords, radius));
    }

    @Operation(summary = "步行路线规划")
    @GetMapping("/walking")
    public R<RouteVO> planWalkingRoute(
            @Parameter(description = "起点经度") @RequestParam Double fromLongitude,
            @Parameter(description = "起点纬度") @RequestParam Double fromLatitude,
            @Parameter(description = "终点经度") @RequestParam Double toLongitude,
            @Parameter(description = "终点纬度") @RequestParam Double toLatitude) {
        LocationDTO origin = new LocationDTO();
        origin.setLongitude(fromLongitude);
        origin.setLatitude(fromLatitude);

        LocationDTO destination = new LocationDTO();
        destination.setLongitude(toLongitude);
        destination.setLatitude(toLatitude);

        return R.ok(mapService.planWalkingRoute(origin, destination));
    }

    @Operation(summary = "IP定位")
    @GetMapping("/ip")
    public R<LocationDTO> getLocationByIp(
            @RequestParam( required = false) String ip) {
        return R.ok(mapService.getLocationByIp(ip));
    }

    @Operation(summary = "计算两点之间的距离")
    @GetMapping("/distance")
    public R<Double> calculateDistance(
            @Parameter(description = "起点经度") @RequestParam Double fromLongitude,
            @Parameter(description = "起点纬度") @RequestParam Double fromLatitude,
            @Parameter(description = "终点经度") @RequestParam Double toLongitude,
            @Parameter(description = "终点纬度") @RequestParam Double toLatitude) {
        return R.ok(mapService.calculateDistance(fromLongitude, fromLatitude, toLongitude, toLatitude));
    }

    //获取IP地址
    @GetMapping("/getIP")
    public R<String> getIp(){
         return R.ok(mapService.getIp());
    }
}
